package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.Chat;
import com.zcc.makingfriend.service.ChatService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zcc
 */
@Api(tags = "消息管理")
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    ChatService chatService;

    @ApiOperation("发送消息")
    @PostMapping("/send")
    public Result send(@RequestBody Chat chat) {
        int i = chatService.sendMessage(chat);
        if (i == 1) {
            return Result.success("发送消息成功");
        }
        return Result.fail("发送消息失败");
    }

    @ApiOperation("查找消息")
    @PostMapping("/find")
    public Result findChat(@RequestBody Chat chat) {
        List<Chat> chats = chatService.findChats(chat);
        if (!chats.isEmpty()) {
            return Result.success(chats);
        }
        return Result.fail("发送消息失败");
    }

    @ApiOperation("阅读未读消息")
    @PostMapping("/readChat")
    public Result readChat(@RequestBody Chat chat) {
        int i = chatService.readChat(chat);
        if (i == 1) {
            return Result.success("阅读成功");
        }
        return Result.fail("阅读失败");
    }
}
